Skip to content

Rename to kilo code in chats#3

Merged
kevinvandijk merged 8 commits into
mainfrom
rename-chat-rows
Mar 10, 2025
Merged

Rename to kilo code in chats#3
kevinvandijk merged 8 commits into
mainfrom
rename-chat-rows

Conversation

@kevinvandijk
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread webview-ui/src/components/chat/Announcement.tsx Outdated
@kevinvandijk kevinvandijk merged commit 5b426a2 into main Mar 10, 2025
@kevinvandijk kevinvandijk deleted the rename-chat-rows branch March 10, 2025 17:28
EamonNerbonne added a commit that referenced this pull request Oct 23, 2025
Xyrces pushed a commit to Xyrces/kilocode_costOptimization that referenced this pull request Nov 16, 2025
…rectional switching

This commit adds intelligent cost optimization to context-based provider switching,
enabling users to stay on free/cheap tiers as long as possible through automatic
compaction and bidirectional model switching.

## New Features

### 1. Compaction-First Strategy
- Automatically attempts conversation compaction before escalating to expensive models
- Tracks compaction attempts per model (default: 3 attempts before switching)
- Each successful compaction keeps the user on the cheaper tier
- Configurable via `preferCompactionOverSwitch` and `maxCompactionAttemptsBeforeSwitch`

### 2. Bidirectional Switching (Downgrades)
- Automatically downgrades to cheaper models after successful compaction
- Checks if token usage fits in previous (cheaper) model's context window
- Triggers downgrade when usage < downgradeThreshold * previousContextWindow (default 50%)
- Configurable via `enableContextBasedDowngrade` and `contextDowngradeThreshold`

### 3. Per-Model Compaction Tracking
- Maintains separate compaction attempt counters for each model
- Resets counter when switching to a different model
- Prevents infinite compaction loops with configurable max attempts

## Configuration Schema Updates

### packages/types/src/provider-settings.ts
Added 5 new configuration parameters:
- `preferCompactionOverSwitch` (boolean, default: true)
- `maxCompactionAttemptsBeforeSwitch` (number, default: 3)
- `autoCompactOnThreshold` (boolean, default: true)
- `contextDowngradeThreshold` (number, 0.0-1.0, default: 0.5)
- `enableContextBasedDowngrade` (boolean, default: true)

## Implementation

### src/api/providers/virtual-quota-fallback.ts
Added bidirectional switching methods:
- `getPreviousProfileInfo()`: Get info about previous (cheaper) profile
- `switchToPreviousProfile()`: Downgrade to previous profile in chain

### src/core/task/Task.ts
Enhanced context switching logic with:
- `getCompactionAttempts(modelId)`: Track compaction attempts per model
- `incrementCompactionAttempts(modelId)`: Increment counter
- `resetCompactionAttempts(modelId)`: Reset counter on model switch
- `attemptAutoCompaction()`: Automatically compact conversation using existing condenseContext()
- `checkContextDowngrade()`: Check and perform downgrade after compaction
- Modified `checkContextThresholdAndSwitch()`: Implement compaction-first flow

### Flow:
1. Hit context threshold (e.g., 80% of 200k)
2. Try compaction (up to 3 attempts on current model)
3. If compaction succeeds: stay on cheap model, increment counter
4. If compaction limit reached: offer expensive model switch
5. After switch: reset compaction counter for new model
6. After successful compaction: check if downgrade to cheaper model is possible
7. If usage fits in cheaper model: automatic downgrade, reset counter

## Testing

### src/core/task/__tests__/Task.compaction-first.spec.ts (NEW)
Comprehensive test coverage (60+ test cases):
- Compaction attempt tracking per model
- Compaction-first strategy flow
- Bidirectional switching (downgrades)
- Integration scenarios (full cost optimization flow)
- Telemetry and logging
- Edge cases (zero context, missing info, errors)

### src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts (NEW)
Bidirectional switching tests (40+ test cases):
- getPreviousProfileInfo() functionality
- switchToPreviousProfile() functionality
- Integration tests (upgrade then downgrade flows)
- Cost optimization scenarios
- Edge cases

## Documentation

### cli/docs/CONTEXT_BASED_SWITCHING.md
Major documentation update:
- Updated overview with compaction-first and bidirectional switching explanation
- Added cost optimization configuration example
- Documented all 5 new configuration parameters
- Added detailed "How It Works" section with flow diagram
- Added example cost-optimized workflow showing savings
- Added "Cost Optimization Best Practices" section
- Added "Tuning for Different Use Cases" with 3 configuration presets
- Updated benefits to emphasize cost savings
- Marked implemented future enhancements

## Cost Impact Example

**Without this feature:**
- Hit 160k/200k threshold → Switch to paid tier ($$$)
- Stay on paid tier for entire conversation

**With this feature:**
- Hit 160k/200k → Compact Kilo-Org#1 → Stay on free tier ✓
- Hit 160k again → Compact Kilo-Org#2 → Stay on free tier ✓
- Hit 160k again → Compact Kilo-Org#3 → Stay on free tier ✓
- Hit 160k again → Switch to paid tier ($$$)
- Compact conversation → Drop to 80k → Automatic downgrade to free tier ✓
- Continue on free tier!

**Result:** Massive cost savings, especially for long-running conversations

## Breaking Changes

None. All new features are opt-in with sensible defaults:
- `preferCompactionOverSwitch`: true (compaction enabled by default)
- `enableContextBasedDowngrade`: true (downgrade enabled by default)
- Existing behavior preserved when disabled

## User Impact

**Positive:**
- Automatic cost optimization with zero configuration required
- Stays on free/cheap tiers longer
- Transparent notifications when compactions/switches occur
- Better cost predictability

**Neutral:**
- Slightly more frequent compactions (but saves money)
- Automatic downgrades (can be disabled if not desired)

## Files Modified
- packages/types/src/provider-settings.ts (5 new config params)
- src/api/providers/virtual-quota-fallback.ts (2 new methods)
- src/core/task/Task.ts (5 new methods, enhanced main logic)
- cli/docs/CONTEXT_BASED_SWITCHING.md (comprehensive update)

## Files Added
- src/core/task/__tests__/Task.compaction-first.spec.ts
- src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts

## Related Issues

This implements the user's feature request for cost optimization through:
1. Compaction before switching to expensive models
2. Configurable compaction attempt limits
3. Automatic downgrade after compaction success

## Technical Notes

- Compaction attempts are tracked in-memory per Task instance
- Counter keys use format: `compaction_attempts_${modelId}`
- Downgrade threshold is percentage of *previous* model's context window
- All operations preserve full conversation history
- Error handling ensures feature never crashes tasks
- Abort checks prevent switches during task cancellation
j-Guru referenced this pull request in j-Guru/kilocode Feb 23, 2026
…-rendering-by-tool-presence

Codex-generated pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants